/**
 * @file
 * @auth jinguangguo
 * @date 2017/1/18
 */

import Vue from 'vue';
import tpl from './tpl.html';
import commonService from '../../../common/commonService';
import service from './service'
import filter from '../../../../grsc-base/util/filter';
import lib from '../../../common/lib';
import Popover from '../../../../grsc-base/component/Popover.vue';
import Loading from '../../../../grsc-base/component/Loading.vue';
import NoData from '../../../../grsc-base/component/NoData.vue';
import Pager from '../../../../grsc-base/component/Pager.vue';
import Qrcode from '../../../../grsc-base/component/Qrcode.vue';
import MathTool from '../../../../grsc-base/util/mathtool';
import AlterPayPwd from '../component/alterPayPwd/main';
import setlogpwd from '../component/modifyLogPassword/modifyLogPassword'
import ResetPayPwd from '../component/resetPayPwd/main';
import resetGoogle from '../component/resetGoogle/main';
import toggleGoogle from '../component/toggleGoogle/main';
import closeGoogleDialog from '../component/closeGoogleDialog/main';
import setPayPwd from '../../../component/setPayPwd/main';
import setNickname from '../../../component/setNickName.vue';
import setPhone from '../../../component/setPhone.vue'

const BASE_UID_NO = 100000;

export default Vue.extend({
    name: "Basic",
    template: tpl,
    components: {
        Popover,
        Qrcode,
        AlterPayPwd,
        ResetPayPwd,
        setlogpwd,
        resetGoogle,
        toggleGoogle,
        Loading,
        NoData,
        Pager,
        closeGoogleDialog,
        setPayPwd,
        setNickname,
        setPhone
    },

    data() {
        return {
            lib: lib,
            uid: '',
            email: '',
            ip: '',
            level: '',
            ipList: null,
            limit: '',
            isSetPayPassword:false,
            nickname:'',
            mobile:'',

            isLoading: false,
            isIPLoading: true,
            isShowAddDialog: false,
            isAlterPayPwd: false,
            isResetPayPwd: false,
            isResetGoogle: false,
            isSetPayPwd:false,
            isSettingNickname:false,
            isSettingPhone:false,

            isToggleGoogle: false, // 控制开关
            hasGoogleAuth: false, // 是否已经验证过
            openGoogleAuth: false, // 开启第一次谷歌验证
            isCloseGoogle:false,// 关闭谷歌验证


            condition: {
                pageNo: 1,
                pageSize: 10,
                pageNum: 1
            }
        }
    },
    created() {
        this.realAuthDetection();
        this.fetchIP();
        this.fetchIPlog();
        this.queryGoogleStatus();
    },
    mounted(){
    },
    methods: {

        //检验用户是否实名认证
        realAuthDetection () {
            this.isLoading = true;
            commonService.basic().done((basicRep) => {
                this.isLoading = false;
                if (basicRep.code === window.CODE.SUCCESS) {
                    let user = basicRep.data;
                    this.email = user.account;
                    this.uid = lib.getFormattedUid(user.uid);
                    this.level = user.authLevel;
                    this.isSetPayPassword = user.setPayPassword;
                    this.nickname = user.nickname;
                    this.mobile = user.mobile;
                    this.fetchWithdrawConfig();
                }
            });
        },

        fetchWithdrawConfig(){
            let key = '';
            switch (this.level) {
                case 'LEVEL0':
                    key = 'WITHDRAWLEVEL_0';
                    break;
                case 'LEVEL1':
                    key = 'WITHDRAWLEVEL_1';
                    break;
                case 'LEVEL2':
                    key = 'WITHDRAWLEVEL_2';
                    break;
            }


            commonService.assetConfigList({key}).done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    let limitArr = [];
                    let noLimit = '';
                    let assetCode;
                    rep.data.forEach((item, index) => {
                        if (+item.profileValue > 0) {
                            assetCode = item.assetCode;
                            limitArr.push(item.profileValue + ' ' + assetCode);
                        } else if (+item.profileValue == 0) {
                            noLimit = ' , ' + this.$t('basic.noOtherlimit');
                        }
                    });

                    this.limit = limitArr.join(' + ') + noLimit;

                } else {
                    window.error(rep.msg)
                }
            })
        },


        /**
         * 获取当前ip
         * */
        fetchIP(){
            let param = {
                pageNo: 1,
                pageSize: 1
            };
            service.ipLog(param).done((rep) => {
                if (rep.code === window.CODE.SUCCESS && rep.data.list.length > 0) {
                    this.ip = rep.data.list[0].ipAddress;
                } else {
                    window.error(rep.msg)
                }
            })
        },

        /**
         * 获取ip地址历史
         * */
        fetchIPlog(){
            this.isIPLoading = true;
            let param = {
                pageNo: this.condition.pageNo,
                pageSize: this.condition.pageSize
            };
            service.ipLog(param).done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    this.ipList = rep.data.list;
                    this.condition.pageNum = Number(filter.ceilFix(rep.data.total / this.condition.pageSize, 0));
                } else {
                    window.error(rep.msg)
                }
                this.isIPLoading = false;
            })
        },

        /**
         * 分页change
         * @param currentPage
         */
        changePage(currentPage) {
            this.condition.pageNo = currentPage;
            this.fetchIPlog();
        },

        copy($event) {
            let target = $event.target;
            lib.copy(target);
        },
        showAlterPayPwd(val){
            this.isAlterPayPwd = true;
        },
        showResetPayPwd(){
            this.isResetPayPwd = true;
        },
        changeShowStatus(val){
            if (val == 'alterPayPwd') {
                this.isAlterPayPwd = false;
            } else if (val == 'resetPayPwd') {
                this.isResetPayPwd = false;
            }
        },
        showAddDialog() {
            this.isShowAddDialog = true;
        },
        changeDigStatus() {
            this.isShowAddDialog = false;
        },

        hideSetSetNickname(){
            this.isSettingNickname = false;
            this.realAuthDetection();
        },

        hideSetPhone(){
            this.isSettingPhone = false;
            this.realAuthDetection();
        },


        /**
         * 谷歌验证码
         * */
        showResetGoogle(){
            this.isResetGoogle = true;
        },

        // 查询用户谷歌验证码开关状态
        queryGoogleStatus(){
            service.googleCodeQuery().done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    window.localStorage.setItem('GOOGLE_BOUND', rep.data.isBound);
                    window.localStorage.setItem('GOOGLE_STATUS', rep.data.status);

                    if (rep.data.isBound === false) { // 没有设置过谷歌验证码
                        this.isToggleGoogle = false;
                        return;
                    } else if (rep.data.isBound === true) {
                        this.hasGoogleAuth = true;
                        if (rep.data.status === 'OFF') { // 开关处于开启状态
                            this.isToggleGoogle = false;
                            return;
                        } else if (rep.data.status === 'ON') {
                            this.isToggleGoogle = true;
                        }
                    }
                } else {
                    window.error(rep.msg);
                }

            }).fail((rep) => {
                window.error(rep)
            })
        },

        // 切换谷歌验证码开关
        toggleGoogle(){
            // this.isToggleGoogle = !this.isToggleGoogle;
            if (this.isToggleGoogle) { // 当前是开启状态，要关闭谷歌验证
                this.updateGoogleStatusOff();
            }else{ // 当前是关闭状态，要开启
                if(this.hasGoogleAuth){ // 已经验证过了谷歌验证码
                    this.updateGoogleStatusOn();
                }else{ // 弹出谷歌验证页面
                    this.openGoogleAuth = true;
                }
            }
        },


        // 非首次开启时谷歌验证码时，用户开启谷歌验证码
        updateGoogleStatusOn(){
            let that = this;
            window.confirm({
                target: that.$refs.switch,
                content: that.$t('basic.securitySetting.openG'),
                confirmBtnText:that.$t('common.yes'),
                cancelBtnText:that.$t('common.no'),
                width:180
            }).done(() => {
                service.googleCodeOn().done((rep) => {
                    if (rep.code === window.CODE.SUCCESS) {
                        //that.isToggleGoogle = false;
                        window.success(this.$t('common.success'));
                    }else{
                        window.error(rep.msg)
                    }
                    this.queryGoogleStatus();
                }).fail((rep)=>{
                    window.error(rep)
                })
            }).fail(() => {

            });
        },

        // 非首次开启时谷歌验证码时，用户关闭谷歌验证码
        updateGoogleStatusOff(){
            this.isCloseGoogle = true;
        },


        //隐藏绑定谷歌绑定窗口
        closeGoogleAuth(){
            this.openGoogleAuth = false;
            this.queryGoogleStatus();
        },


        closeGoogleDialog(){
            this.isCloseGoogle = false;
            this.queryGoogleStatus();
        },


        setPayPwdSuccess(){
            this.isSetPayPassword = true;
        }

    },
    watch: {}
});
